<?php
class LeaderBoardRecord{
	public $ID;
	public $Score = 0;
	function __construct($userid,$score){
		$this->ID = $userid;
		$this->Score = $score;
	}
	static function swap($bytebuffer){
		$obj = new LeaderBoardRecord(DataUtil::readInt32( $bytebuffer ) ,DataUtil::readInt32( $bytebuffer ) );
		return $obj;
	}
	function getUserID(){
		return $this->ID;
	}
	function getScore(){
		return $this->Score;
	}
	function getBinSize(){
		$size = 0;
		$size += 4 ;
		$size += 4 ;
		return $size;
	}
	function getBinary(){
		$buf = ByteBuffer::allocate( $this->getBinSize() ) ;
		DataUtil::writeInt32( $buf , $this->ID ) ;
		DataUtil::writeInt32( $buf , $this->Score ) ;
		return  $buf ->_array() ;
	}
}
class DataLeaderBoard{
	private $Records = array();
	private $Limit = 0;
	private $Revision = 0;
	function __construct($limit){
		$this->Limit = $limit;
	}
	private function updateRevision(){
		$this->Revision = ($this->Revision + 1)% 500000;
	}
	static function swap($bytebuffer){
		$obj = new DataLeaderBoard(DataUtil::readInt16( $bytebuffer ) );
		$obj->Revision = DataUtil::readInt32( $bytebuffer ) ;
		$num_of_record = DataUtil::readInt16( $bytebuffer ) ;
		for($i = 0; $i< $num_of_record; $i++){
			$obj->Records[] = LeaderBoardRecord::swap($bytebuffer);
		}
		return $obj;
	}
	function getLeaders(){
		return $this->Records;
	}
	function getRevision(){
		return $this->Revision;
	}
        function putSocreTemp($userid, $score)
        {
            $fp= fopen("text.txt",a) or exit("Khong tim thay file can mo");
            fwrite($fp, " ------------------------------------------------ \n");
            fwrite($fp, " putSocreTemp \n");
            if(is_array($this->Records)){
			$this->Records = array_values($this->Records);
			$length = count($this->Records);
			if($length >= $this->Limit){
				if($score < $this->Records[0]->getScore()){
					return false;
				}
			}
                        $userpos = -1;
			for($i = 0; $i< $length; $i++){
                            if($this->Records[$i]->getUserID() == $userid)  
                            {
                                $userpos = $i;
                                break;
                            }
			}
                        if($userpos != -1)
                        {
                            if($this->Records[$userpos]->getScore()>= $score)
                            {
                                return false;
                            }
                            else 
                            {
                                for($i = $userpos; $i< $length; $i++){
                                    if($this->Records[$i]->getScore() >= $score){
                                          fwrite($fp, " >>>>>>>>>>>>>>>>>>>>>>>>>. \n");
                                          for($j = $userpos; $j < $i - 1; $j++){
                                                  $this->Records[$j] = $this->Records[$j+1];						
                                          }
                                          $this->Records[$i - 1] = new LeaderBoardRecord($userid,$score);
                                          $this->updateRevision();
                                          return true;
                                   }
                                }
                                fwrite($fp, " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<. \n");
                                for($j = $userpos; $j < $length-1; $j++){
                                          $this->Records[$j] = $this->Records[$j+1];						
                                }
                                $this->Records[$length - 1] = new LeaderBoardRecord($userid,$score);
                                $this->updateRevision();
                                return true;     
                            }
                        }
                        else
                        {
                            for($i = 0; $i< $length; $i++){
                                if($this->Records[$i]->getScore() >= $score){
					if($length < $this->Limit){
						$this->Records[] = null;
						for($j = $length-1; $j>= $i; $j--){
							$this->Records[$j + 1] = $this->Records[$j];
						}
						$this->Records[$i] = new LeaderBoardRecord($userid,$score);
					}else if($i >= 1){
						for($j = 0; $j < $i - 1; $j++){
							$this->Records[$j] = $this->Records[$j+1];						
						}
						$this->Records[$i - 1] = new LeaderBoardRecord($userid,$score);
					}
					$this->updateRevision();
					return true;
				}
                            }
                        }
			if($length < $this->Limit){
				$this->Records[] = new LeaderBoardRecord($userid,$score);
			}else{
				for($j = 0; $j < $length - 1; $j++){
					$this->Records[$j] = $this->Records[$j+1];						
				}
				$this->Records[$length - 1] = new LeaderBoardRecord($userid,$score);
			}
			$this->updateRevision();
			return true;
		}
                fclose($fp);
		return false;
        }
	function putScore($userid,$score){
		if(is_array($this->Records)){
			$this->Records = array_values($this->Records);
			$length = count($this->Records);
			if($length >= $this->Limit){
				if($score < $this->Records[0]->getScore()){
					return false;
				}
			}
			for($i = 0; $i< $length; $i++){
				if($this->Records[$i]->getScore() >= $score){
					if($length < $this->Limit){
						$this->Records[] = null;
						for($j = $length-1; $j>= $i; $j--){
							$this->Records[$j + 1] = $this->Records[$j];
						}
						$this->Records[$i] = new LeaderBoardRecord($userid,$score);
					}else if($i >= 1){
						for($j = 0; $j < $i - 1; $j++){
							$this->Records[$j] = $this->Records[$j+1];						
						}
						$this->Records[$i - 1] = new LeaderBoardRecord($userid,$score);
					}
					$this->updateRevision();
					return true;
				}
			}
			if($length < $this->Limit){
				$this->Records[] = new LeaderBoardRecord($userid,$score);
			}else{
				for($j = 0; $j < $length - 1; $j++){
					$this->Records[$j] = $this->Records[$j+1];						
				}
				$this->Records[$length - 1] = new LeaderBoardRecord($userid,$score);
			}
			$this->updateRevision();
			return true;
		}
		return false;
	}
	function getBinSize(){
		$size = 0;
		$size += 2 ;
		$size += 4 ;
		$size += 2 ;
		if(is_array($this->Records)){
			foreach($this->Records as $record){
				$size += $record->getBinSize();
			}
		}
		return $size;
	}
	function getBinary(){
		$buf = ByteBuffer::allocate( $this->getBinSize() ) ;
		DataUtil::writeInt16( $buf , $this->Limit ) ;
		DataUtil::writeInt32( $buf , $this->Revision ) ;
		DataUtil::writeInt16( $buf , count($this->Records) ) ;
		if(is_array($this->Records)){
			foreach($this->Records as $record){
				DataUtil::writeBytes( $buf , $record->getBinary() ) ;
			}
		}
		return  $buf ->_array() ;
	}
}
?>